$(function(){
	var editors;
	var exp_editors;
	$.fn.ifEmpty = function(cb){
	    var _this = this;
	    setInterval(function(){
	        _this.each(function(index, el) {
	            var el = $(el);
	            if(!el.html().length || !el.html().replace(new RegExp('[\s\n\r\t ]+', 'g'), '').length){
	                return cb(el);
	            }
	        });
	    }, 50);
	};
});

function loading(){
	$('.ajax-loading').each(function(){
        var target = $(this);
        target.html('<div class="loader-wrapper"><div class="plus-loader"></div></div>');
        setTimeout(function(){
            target.find('.loader-wrapper').remove();
        }, 5000);
    });
}
function isComEmpty(data){
	if(isNaN(data) || typeof(data) == 'undefined' || data =='' || data == '0' || data == 0){
		return true;
	} 
	return false;
}

function inintIcheck(){
	$('input').iCheck({
		checkboxClass: 'icheckbox_square-green',
		radioClass: 'iradio_square-green',
		increaseArea: '-10' // optional
	});
};
function getOptions(type) {
	var nowVal = parseInt($('input[type='+type+']').last().val());
	
	return nowVal+1;
};
function changeIntToEng(n) {
	switch(n)
	{
	case 1:
		return 'A、';
	case 2:
		return 'B、';
	case 3:
		return 'C、';
	case 4:
		return 'D、';
	case 5:
		return 'E、';
	case 6:
		return 'F、';
	case 7:
		return 'G、';
	case 8:
		return 'H、';
	case 9:
		return 'I、';
	case 10:
		return 'J、';
	}
};
function changeIntToEng2(n) {
	switch(n)
	{
	case 1:
		return 'A';
	case 2:
		return 'B';
	case 3:
		return 'C';
	case 4:
		return 'D';
	case 5:
		return 'E';
	case 6:
		return 'F';
	case 7:
		return 'G';
	case 8:
		return 'H';
	case 9:
		return 'I';
	case 10:
		return 'J';
	}
};
/**
 * 改变问题模版
 */
function changeQuestion(type) {
	var container = $('#question-option-container');
	var optionTemplate = _.template($("#"+type+"-init-template").html());
	
	container.html(optionTemplate);
	
	inintIcheck();
	questionEditorInit();
}

function questionEditorInit(){
	KindEditor.ready(function(K) {
		editors = K.create('.question_content', {
			resizeType : 1,
			//allowPreviewEmoticons : false,
			afterBlur: function () { this.sync(); },
			allowImageUpload : true,
			width : "100%", 
			allowUpload : true,
			minHeight:50,
			uploadJson : '/upload/images?dest_dir=questions',
			items : ["justifyleft","image"]
		});
		exp_editors = K.create('.expain_content', {	
			resizeType : 1,
			//allowPreviewEmoticons : false,
			afterBlur: function () { this.sync(); },
			allowImageUpload : true,
			width : "100%", 
			allowUpload : true,
			minHeight:100,
			uploadJson : '/upload/images?dest_dir=questions',
			items : ["justifyleft","image"]
		});
		//editors.html('');
		//exp_editors.html('');
	});
};

function formDataParse(type) {
	var options = new Array();
	if(type==3){
		options[0] = '论述题';
	}else{
		$("textarea[name^=option_").each(function(tk,te){
			var dataValue = $(this).attr('data-value');
			options[tk] = dataValue + "、" + '<div style="display:inline-block;">' + $(this).val() + '</div>';
		});
	}
	return options;
};

function error(erro){
	var response = eval('(' + erro.responseText + ')');
	var errors = '';
    $.each(response,function(k,v){
        errors +="<div>"+v+"</div>";
    })
    layer.open({
        title: '系统提示',
        content: errors
    });
}
function initSelect(){
	$('select:not(.origin)').selectpicker({
		style: 'btn-info',
	}).on('change', function(){
	    $(this).selectpicker({
	    	style: 'btn-info',
	    });
	});
}
/**
 * 消息详情
 */
function messageDetail(url,id)
{
    $.ajax({
        'url':url,
        'type':'GET',
        error: function(data) {
            layer.open({title:'提示',content:'网络异常'});
        },
        success: function(data){
            if(data.status == 'success')
            {
                var result = data.data;
                var html = "";
                $.each(result.data,function(k,v){
                    html += '<div class="talk_time">'+v.created_at+'</div>';
                    if(v.user_id != result.uid){
                        html += '<div class="ml-10 fl">'
                            +'<img src="'+result.otherLogo+'" width="50" height="50" style="border-radius: 25px;width: 50px; height: 50px;">'
                            +'<i class="glyphicon glyphicon-triangle-left" style="color:rgb(246,246,246);top:3px;left: -2px"></i>'
                            +'<div class="talk_left_d word">'
                            +'	<span class="talk_left_s">'+v.message+'</span>'
                            +'</div>'
                            +'</div><div class="clearfix"></div>';
                    }else{
                        html += '<div class="mr-10 fr">'
                            +'<img src="'+result.selfLogo+'" width="50" height="50" style="border-radius: 25px;width: 50px; height: 50px;" class="fr">'
                            +'<i class="glyphicon glyphicon-triangle-right" style="color:rgb(246,246,246);top: 20px;left: 105px;"></i>'
                            +'<div class="talk_right_d word">'
                            +'	<span class="talk_right_s">'+v.message+'</span>'
                            +'</div>'
                            +'</div><div class="clearfix"></div>';
                    }
                });
                html +="<input type='hidden' name='rid' value='"+result.rid+"'><input type='hidden' name='id' value='"+result.id+"'>";
                $('#myModalLabel').html(result.recipientname);
                $('#messge_send').attr('onclick','sendMessage("/company/center/message","messageForm")');
                $('#message_content').html(html);
                $('#myMessageDetail').modal();
                $('#unread_'+id).html(0);
            }
        }
    });
}
/**
 * 发送消息
 */
function sendMessage(url,formid)
{
    var data = $('#'+formid).serialize();
    $.ajax({
        'url':url,
        'type':'POST',
        'data':data,
        error: function(data) {
            layer.open({title:'提示',content:'网络异常'});
        },
        success: function(data){
            if(data.status == 'success'){
                var result = data.data;
                var html = '<div class="talk_time">'+result.created_at+'</div>';
                html += '<div class="mr-10 fr">'
                    +'<img src="'+result.selfLogo+'" width="50" height="50" style="border-radius: 25px;width: 50px; height: 50px;" class="fr">'
                    +'<i class="glyphicon glyphicon-triangle-right" style="color:rgb(246,246,246);top: 20px;left: 105px;"></i>'
                    +'<div class="talk_right_d word">'
                    +'	<span class="talk_right_s">'+result.message+'</span>'
                    +'</div>'
                    +'</div><div class="clearfix"></div>';
                $("#message_content").append(html);
                $("#msgContent").val('');
            }
            else
            {
                layer.open({title:'提示',content:data.msg});
            }
        }
    });
}
/**
 * 删除
 * @param url 删除路径
 * @param id 文章id
 * @param token 
 */
function delete_one(url,id,token){
    layer.confirm('确认删除',{title:'确定要删除吗?'},function(result){
            if(result) {
                $.ajax({
                    'url':url,
                    'type':'DELETE',
                    'data':'_token='+token,
                    success: function(data){
                        if(data.status == 'success'){
                            layer.open({title:'提示',content:data.msg});
                            $("#news_"+id).remove();
                        }else{
                            layer.open({title:'提示',content:data.msg});
                        }

                    },
                });
            }else{
                layer.open({title:'提示',content:'删除操作已取消'});
            }
    });
}
function questions(id,curr,questionsTemplate,container){
	var data = {
			itembank_id:id,
			page:curr
	}
	$('.loading').show();
	$.ajax({
		'url':'/biz/index/questions',
		'type':'GET',
		'dataType':'json',
		'data':data,
		success: function(data){
			$('.loading').hide();
			var html="";
			$.each(data.data,function(k,v){
        		html += questionsTemplate({item:v});
        	});
			laypage({
		            cont: 'holder', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
		            pages: data.last_page, //通过后台拿到的总页数
		            curr: data.current_page || 1, //当前页
		            skin: 'molv', //皮肤
		            first: 1, //将首页显示为数字1,。若不显示，设置false即可
		            last: data.last_page, //将尾页显示为总页数。若不显示，设置false即可
		            prev: '<', //若不显示，设置false即可
		            next: '>',
		            jump: function(obj,first){ //触发分页后的回调
		            	 if(!first){ //一定要加此判断，否则初始时会无限刷新
		            		 questions(id,obj.curr,questionsTemplate,container);
		                 }
		            }
	        });
			
			container.html(html);
		}
	});
}
function selectInit(){
	$('select').selectpicker({
        style: 'btn-transparent',
    });
}

function isEmpty(data){
	if(isNaN(data) || typeof(data) == 'undefined' || data =='' || data == '0' || data == 0){
		return true;
	} 
	return false;
}

function getFormValue(nowForm,id){
	return nowForm.find('input[name='+id+']').val();
}